package com.egao.api.controller.orderB;


import com.egao.api.base.BaseApiController;
import com.egao.common.core.annotation.OperLog;
import com.egao.common.core.web.JsonResult;
import com.egao.common.core.web.PageResult2;
import com.egao.common.system.entity.OperRecord;
import com.egao.common.system.entity.User;
import com.egao.common.system.service.OperRecordService;
import com.egao.orderB.entity.Address;
import com.egao.orderB.entity.Bill;
import com.egao.orderB.entity.MaintainFee;
import com.egao.orderB.service.AddressService;
import com.egao.orderB.service.BillService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONObject;
import org.apache.poi.ss.formula.functions.T;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

//import com.egao.orderB.entity.Bill;
//import com.egao.orderB.service.BillService;

/**
 * api登录相关接口
 * Created by wangfan on 2019-05-29 下午 3:43.
 * http://localhost/swagger-ui.html
 */
@RequestMapping("/api/address")
@RestController
@Api(value = "客户运费报价",tags = {"客户运费报价"})
public class ApiAddressController extends BaseApiController {

    @Autowired
    private OperRecordService operRecordService;
    @Autowired
    private AddressService addressService;



    /**
     * 运费维护列表
     */
    @PostMapping("/list")
    @ApiOperation(value = "客户运费报价列表",notes = "" +
            "limit:每页显示几行 必填\n" +
            "page:第几页 必填\n" +
            "customerId:客户id \n" +
            "customerName:客户名称\n" +
            "province:省\n" +
            "city:市\n" +
            "district:县区\n" +
            "town:镇\n" +
            "address:详细地址\n" +
            "dock:码头\n" +
            "boxType:箱型\n" +
            "返回的字段:\n" +
            "id:id\n" +
            "companyId:公司id\n" +
            "customerId:客户id\n" +
            "customerName:客户名称\n" +
            "province:省\n" +
            "city:市\n" +
            "district:县区\n" +
            "town:镇\n" +
            "address:详细地址\n" +
            "dock:码头\n" +
            "boxType:箱型\n" +
            "price:价格\n" +
            "contact:联系人\n" +
            "contactPhone:联系电话\n" +
            "state:状态\n" +
            "remark:备注\n" +
            "createTime:创建时间\n" +
            "createUser:创建人\n" +
            "updateTime:更新时间\n" +
            "updateUser:更新人\n" +
            "deleted:del(0正常 1删除)\n" +
            "",httpMethod = "POST")
    @ResponseBody
    public JSONObject list(PageResult2<T> page, Address address, HttpServletRequest request)  throws Exception{
        operRecordService.saveAsync(new OperRecord(Integer.valueOf(getLoginUserId(request)), "客户运费报价列表", "客户运费报价列表",
                request.getServletPath(), "POST", "ApiMaintainFeeController.list", request.getQueryString(),//JSONObject.toJSON(bill).toString(),
                "", getIpAddress(request)));
        return addressService.list(page, address, getLoginUser(request));
    }


    /**
     * 查找客户运费报价
     * 如果没有找到就去maintainFee表里去找
     */
    @PostMapping("/searchAddressFee")
    @ApiOperation(value = "查找客户运费报价",notes = "" +
            "customerId:查询字段\n" +
            "customerName:查询字段\n" +
            "返回的字段:\n" +
            "id:id\n" +
            "companyId:公司id\n" +
            "customerId:客户id\n" +
            "customerName:客户名称\n" +
            "province:省\n" +
            "city:市\n" +
            "district:县区\n" +
            "town:镇\n" +
            "address:详细地址\n" +
            "dock:码头\n" +
            "boxType:箱型\n" +
            "price:价格\n" +
            "contact:联系人\n" +
            "contactPhone:联系电话\n" +
            "state:状态\n" +
            "remark:备注\n" +
            "",httpMethod = "POST")
    @ResponseBody
    public JsonResult searchAddressFee(Address address, HttpServletRequest request)  throws Exception{
        operRecordService.saveAsync(new OperRecord(Integer.valueOf(getLoginUserId(request)), "查找客户运费报价", "查找客户运费报价",
                request.getServletPath(), "POST", "ApiMaintainFeeController.searchAddressFee", request.getQueryString(),//JSONObject.toJSON(bill).toString(),
                "", getIpAddress(request)));
        return JsonResult.ok().setData(addressService.searchAddressFee(address, getLoginUser(request)));
    }




    /**
     * 添加运费报价
     */
    @PostMapping("/sszgSave")
    @ApiOperation(value = "平台 添加客户运费报价",notes = "\n" +
            "customerId:客户id\n" +
            "customerName:客户名称\n" +
            "province:省\n" +
            "city:市\n" +
            "district:县区\n" +
            "town:镇\n" +
            "address:详细地址\n" +
            "dock:码头\n" +
            "boxType:箱型\n" +
            "price:价格\n" +
            "contact:联系人\n" +
            "contactPhone:联系电话\n" +
            "remark:备注\n" +
            "",httpMethod = "POST")
    @ResponseBody
    public JsonResult sszgSave(Address address, HttpServletRequest request) {
        operRecordService.saveAsync(new OperRecord(Integer.valueOf(getLoginUserId(request)), "平台 添加客户运费报价", "平台 添加客户运费报价",
                request.getServletPath(), "POST", "ApiAddressController.sszgSave", request.getQueryString(),//JSONObject.toJSON(bill).toString(),
                "", getIpAddress(request)));
        address.setState("已审核");
        return addressService.save(address, getLoginUser(request));
    }

    /**
     * 添加运费报价
     */
    @PostMapping("/customerSave")
    @ApiOperation(value = "客户 添加客户运费报价",notes = "\n" +
            "customerId:客户id 必填\n" +
            "province:省\n" +
            "city:市\n" +
            "district:县区\n" +
            "town:镇\n" +
            "address:详细地址\n" +
            "dock:码头\n" +
            "boxType:箱型\n" +
            "contact:联系人\n" +
            "contactPhone:联系电话\n" +
            "remark:备注\n" +
            "",httpMethod = "POST")
    @ResponseBody
    public JsonResult customerSave(Address address, HttpServletRequest request) {
        operRecordService.saveAsync(new OperRecord(Integer.valueOf(getLoginUserId(request)), "客户 添加客户运费报价", "客户 添加客户运费报价",
                request.getServletPath(), "POST", "ApiAddressController.customerSave", request.getQueryString(),//JSONObject.toJSON(bill).toString(),
                "", getIpAddress(request)));
        address.setState("申请");
        address.setPrice("");
        return addressService.save(address, getLoginUser(request));
    }

    /**
     * 修改运费报价
     */
    @PostMapping("/update")
    @ApiOperation(value = "平台 修改客户运费报价",notes = "" +
            "id:运费报价id 必填\n" +
            "province:省\n" +
            "city:市\n" +
            "district:县区\n" +
            "town:镇\n" +
            "address:详细地址\n" +
            "dock:码头\n" +
            "boxType:箱型\n" +
            "price:价格\n" +
            "contact:联系人\n" +
            "contactPhone:联系电话\n" +
            "remark:备注\n" +
            "",httpMethod = "POST")
    @ResponseBody
    public JsonResult update(Address address, HttpServletRequest request) {
        operRecordService.saveAsync(new OperRecord(Integer.valueOf(getLoginUserId(request)), "修改客户运费报价实体类", "修改客户运费报价实体类",
                request.getServletPath(), "POST", "ApiAddressController.update", request.getQueryString(),//JSONObject.toJSON(bill).toString(),
                "", getIpAddress(request)));
        return addressService.update(address, getLoginUser(request));
    }

    /**
     * 修改运费报价
     */
    @PostMapping("/customerUpdate")
    @ApiOperation(value = "客户 修改客户运费报价",notes = "" +
            "id:运费报价id 必填\n" +
            "province:省\n" +
            "city:市\n" +
            "district:县区\n" +
            "town:镇\n" +
            "address:详细地址\n" +
            "dock:码头\n" +
            "boxType:箱型\n" +
            "contact:联系人\n" +
            "contactPhone:联系电话\n" +
            "remark:备注\n" +
            "",httpMethod = "POST")
    @ResponseBody
    public JsonResult customerUpdate(Address address, HttpServletRequest request) {
        operRecordService.saveAsync(new OperRecord(Integer.valueOf(getLoginUserId(request)), "修改客户运费报价实体类", "修改客户运费报价实体类",
                request.getServletPath(), "POST", "ApiAddressController.customerUpdate", request.getQueryString(),//JSONObject.toJSON(bill).toString(),
                "", getIpAddress(request)));
        return addressService.customerUpdate(address, getLoginUser(request));
    }

    /**
     * 修改运费报价(price) 不需要了
     */
    /*@PostMapping("/updatePrice")
    @ApiOperation(value = "修改客户运费报价(price)",notes = "" +
            "id:运费报价id 必填\n" +
            "price:价格\n" +
            "",httpMethod = "POST")
    @ResponseBody*/
    public JsonResult updatePrice(Address address, HttpServletRequest request) {
        operRecordService.saveAsync(new OperRecord(Integer.valueOf(getLoginUserId(request)), "修改客户运费报价(price)", "修改客户运费报价(price)",
                request.getServletPath(), "POST", "ApiAddressController.updatePrice", request.getQueryString(),//JSONObject.toJSON(bill).toString(),
                "", getIpAddress(request)));
        return addressService.updatePrice(address, getLoginUser(request));
    }

    /**
     * 审核运费报价状态(state)
     */
    @PostMapping("/updateState")
    @ApiOperation(value = "审核运费报价状态",notes = "" +
            "id:运费报价id 必填\n" +
            "",httpMethod = "POST")
    @ResponseBody
    public JsonResult updateState(Address address, HttpServletRequest request) {
        operRecordService.saveAsync(new OperRecord(Integer.valueOf(getLoginUserId(request)), "审核运费报价状态", "审核运费报价状态",
                request.getServletPath(), "POST", "ApiAddressController.updateState", request.getQueryString(),//JSONObject.toJSON(bill).toString(),
                "", getIpAddress(request)));
        return addressService.updateState(address, getLoginUser(request));
    }

    /**
     * 删除运费报价
     */
    @PostMapping("/remove")
    @ApiOperation(value = "删除客户运费报价",notes = "id:id必填",httpMethod = "POST")
    @ResponseBody
    public JsonResult remove(Address address, HttpServletRequest request) {
        operRecordService.saveAsync(new OperRecord(Integer.valueOf(getLoginUserId(request)), "删除客户运费报价", "删除客户运费报价",
                request.getServletPath(), "POST", "ApiAddressController.remove", request.getQueryString(),//JSONObject.toJSON(bill).toString(),
                "", getIpAddress(request)));
        return addressService.remove(address, getLoginUser(request));
    }

}
